/**
 * 链表
 *
 * @author Kevin
 * @date 2018-11-22
 */
public class LinkedList<E> {

    /**
     * 虚拟头节点
     */
    private Node dummyHead;

    /**
     * 链表中的元素个数
     */
    private int size;

    public LinkedList() {
        this.dummyHead = new Node();
        this.size = 0;
    }

    /**
     * 获取链表中的元素个数
     */
    public int getSize() {
        return this.size;
    }

    /**
     * 链表是否为空
     */
    public boolean isEmpty() {
        return this.size == 0;
    }

    /**
     * 在链表头添加新的元素e
     */
    public void addFirst(E e) {
        add(0, e);
    }

    /**
     * 在链表的index(0-based)位置添加新的元素e
     */
    public void add(int index, E e) {
        if (index < 0 || index > this.size) {
            throw new IllegalArgumentException("Add failed. Illegal index.");
        }

        Node prev = this.dummyHead;

        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }

        prev.next = new Node(e, prev.next);
        this.size++;
    }

    /**
     * 在链表末尾添加新的元素e
     */
    public void addLast(E e) {
        add(this.size, e);
    }

    /**
     * 获得链表的第index(0-based)个位置的元素
     */
    public E get(int index) {
        if (index < 0 || index >= this.size) {
            throw new IllegalArgumentException("Get failed. Illegal index.");
        }

        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }

        return cur.e;
    }

    /**
     * 获得链表的第一个元素
     */
    public E getFirst() {
        return get(0);
    }

    /**
     * 修改链表的第index(0-based)个位置的元素为e
     */
    public void set(int index, E e) {
        if (index < 0 || index >= this.size) {
            throw new IllegalArgumentException("Set failed. Illegal index.");
        }

        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }

        cur.e = e;
    }

    /**
     * 获得链表的最后一个元素
     */
    public E getLast() {
        return get(this.size - 1);
    }

    /**
     * 查找链表中是否有元素e
     */
    public boolean contains(E e) {
        Node cur = dummyHead.next;
        while (cur != null) {
            if (cur.e.equals(e)) {
                return true;
            }

            cur = cur.next;
        }

        return false;
    }

    /**
     * 从链表中删除index(0-based)位置的元素，返回删除的元素
     *
     * @param index
     * @return
     */
    public E remove(int index) {
        if (index < 0 || index >= this.size) {
            throw new IllegalArgumentException("remove failed. Illegal index.");
        }

        Node prev = this.dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }

        Node retNode = prev.next;
        prev.next = retNode.next;
        retNode.next = null;
        this.size--;

        return retNode.e;
    }

    /**
     * 从链表中删除第一个元素，返回删除的元素
     *
     * @return
     */
    public E removeFirst() {
        return remove(0);
    }

    /**
     * 从链表中删除最后一个元素，返回删除的元素
     *
     * @return
     */
    public E removeLast() {
        return remove(this.size - 1);
    }

    /**
     * 从链表中删除元素e
     */
    public void removeElement(E e) {
        Node prev = this.dummyHead;
        while (prev.next != null) {
            if (prev.next.e.equals(e)) {
                break;
            }

            prev = prev.next;
        }

        if (prev.next != null) {
            Node delNode = prev.next;
            prev.next = delNode.next;
            delNode.next = null;
            this.size--;
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();

        for (Node cur = dummyHead.next; cur != null; cur = cur.next) {
            sb.append(cur + "->");
        }

        sb.append("NULL");

        return sb.toString();
    }

    /**
     * 节点类
     */
    private class Node {
        public E e;

        public Node next;

        public Node() {
            this(null, null);
        }

        public Node(E e) {
            this(e, null);
        }

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        @Override
        public String toString() {
            return e.toString();
        }

    }

}
